﻿using System.Collections.Generic;
using System;
//还需要将orders文件添加到单元测试项目的引用中
namespace TestProject1
{
    [TestClass]
    public class OrderServiceTests
    {
        private OrderService _orderService;
        [TestInitialize]
        public void setup()
        {
            _orderService = new OrderService();
        }

        [TestMethod]
        [ExpectedException(typeof(Exception), "Order already exists.")]
        public void AddOrder_WhenOrderNotExists_AddsOrder()
        {
            var order = new Order { OrderId = 1, Customer = "Test Order" };
            _orderService.AddOrder(order);
            _orderService.AddOrder(order);
        }

        [TestMethod]
        [ExpectedException(typeof(Exception), "Order does not exist.")]
        public void RemoveOrder_NotExist()
        {
            var order = new Order { OrderId = 2, Customer = "Test Order" };
            _orderService.RemoveOrder(1);
        }
        [TestMethod]
        [ExpectedException(typeof(Exception), "Order does not exist.")]
        public void UpdateOrder_Nomatch()
        {
            var order = new Order { OrderId = 1, Customer = "Test Order" };
            _orderService.AddOrder(order);
            order = new Order { OrderId = 2, Customer = "Test Order" };
            _orderService.UpdateOrder(order);

        }
        public void QueryOrders_intial()
        {
            var _orders = new List<Order>
            {
            new Order { OrderId=1, Customer = "Alice", Details = new List<OrderDetails> { new OrderDetails { ProductName = "Apple", Price = 10, Quantity=1 } } },
            new Order { OrderId=2,Customer = "Bob", Details = new List<OrderDetails> { new OrderDetails { ProductName = "Banana", Price = 5, Quantity = 1 } } },
            new Order { OrderId=3,Customer = "Alice", Details = new List<OrderDetails> { new OrderDetails { ProductName = "Apple", Price = 5, Quantity = 3 } } }
             };
            foreach (var _order in _orders)
            {
                _orderService.AddOrder(_order);
            }
        }
        [TestMethod]
        public void QueryOrdersByCustomer_Test()
        {
            QueryOrders_intial();
            var results = _orderService.QueryOrdersByCustomer("Alice").ToList();
            Assert.AreEqual(2, results.Count);
            Assert.IsTrue(results.All(o => o.Customer == "Alice"));
            Assert.IsTrue(results.First().GetTotalAmount() <= results.Last().GetTotalAmount());
        }
        [TestMethod]
        public void QueryOrdersByProduct_Test()
        {
            QueryOrders_intial();
            var results = _orderService.QueryOrdersByProduct("Apple").ToList();
            Assert.AreEqual(2, results.Count);
            Assert.IsTrue(results.All(o => o.Details.Any(d => d.ProductName == "Apple")));
            Assert.IsTrue(results.First().GetTotalAmount() <= results.Last().GetTotalAmount());
        }
        public void sort_intial()
        {
            QueryOrders_intial();
            var _orders = new List<Order>
            {
            new Order { OrderId=10, Customer = "Kite", Details = new List<OrderDetails> { new OrderDetails { ProductName = "Apple", Price = 20, Quantity=2 } } },
            new Order { OrderId=6,Customer = "Eurake", Details = new List<OrderDetails> { new OrderDetails { ProductName = "Banana", Price = 5, Quantity = 1 },new OrderDetails{ ProductName = "Apple", Price = 5, Quantity = 3 } } },
            new Order { OrderId=4,Customer = "Lisa", Details = new List<OrderDetails> { new OrderDetails { ProductName = "Apple", Price = 5, Quantity = 6 } } }
             };
            foreach (var _order in _orders)
            {
                _orderService.AddOrder(_order);
            }
        }
        [TestMethod]
        public void SortOrders_ByName()//自定义排序
        {
            sort_intial();
            _orderService.SortOrders(o => o.Customer[0]);
            var orders = _orderService.GetOrders();
            bool isInOrder = true;
            for (int i = 1; i < orders.Count; i++)
            {
                if (orders[i - 1].Customer[0] > orders[i].Customer[0])
                {
                    isInOrder = false;
                    break;
                }
            }
            Assert.IsTrue(isInOrder);
        }
        [TestMethod]
        public void SortOrders_ByName(Func<Order, object> keySelector)//默认排序
        {
            sort_intial();
            _orderService.SortOrders();
            var orders = _orderService.GetOrders();
            bool isInOrder = true;
            for (int i = 1; i < orders.Count; i++)
            {
                if (orders[i - 1].OrderId > orders[i].OrderId)
                {
                    isInOrder = false;
                    break;
                }
            }
            Assert.IsTrue(isInOrder);
        }
    }
}